/********************************************************************************************************
/*
/*    regex.cpp
/*
/********************************************************************************************************/

//-------------------------------------------------------------------------------------------------------
//  MEMORY DEBUG
//-------------------------------------------------------------------------------------------------------

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

//-------------------------------------------------------------------------------------------------------
//  INCLUDES
//-------------------------------------------------------------------------------------------------------
#include <regex>
#include "tStr.h"

using namespace std::tr1;  // change namespace to boost and link to boost regex library
                           // if your compiler doesn't support regex.


//----------------------------------------------------------------------------------------------------------------------
//  GET NEXT MATCH
//  *ppNext is the haystack
//----------------------------------------------------------------------------------------------------------------------
bool get_next_match ( cStrW aRet[], int iArrayQty, const wchar_t ** ppNext, const wchar_t * pRegex  )
{
	if ( ! ppNext )
		return false;

	if ( ! *ppNext )
		return false;
	
	wregex  rgx ( pRegex );
	wcmatch bmatch;

	if ( regex_search ( *ppNext, bmatch, rgx) )
	{
		for ( int i = 0; i < iArrayQty; i++ )
			aRet[i] =  static_cast<std::wstring>(bmatch[i+1]).c_str();

		*ppNext =  bmatch [ bmatch.size() ] . second;

		return true;
	}

	return false;
}



//-------------------------------------------------------------------------------------------------------
//  MATCH
//-------------------------------------------------------------------------------------------------------
bool match ( cStrW  aRet [], int iRetSize, const wchar_t * pHaystack, const wchar_t * pRgx )
{
	wregex  rgx ( pRgx );
	wcmatch bmatch;

	if ( regex_match ( pHaystack, bmatch, rgx ) )
	{
		for ( int i = 0; i < iRetSize; i++ )
		{
			aRet[i] = (static_cast<std::wstring>(bmatch[i+1])).c_str();
		}

		return true;
	}

	return false;
}
